<template>
  <div>
    <div class="login">
      <div class="login-head">
        <span>注册</span>
      </div>
      <div class="login-content">
        <div class="usercount">
          <input type="text" v-model="reuser.name" placeholder="请输入账号"/>
        </div>
        <div class="password">
          <input type="text" v-model="reuser.password" placeholder="请输入密码"/>
        </div>
        <div class="email">
          <input type="text" v-model="reuser.email" placeholder="请邮箱账号"/>
        </div>
        <div class="checkcode">
          <input type="text" v-model="reuser.checkcode" placeholder="验证码">
          <span @click="sendemail" v-show="isfirstsend">发送验证码</span>
          <span v-show="isrestime">{{restime}}秒发送</span>
          <span v-show='isresend' @click="sendemail">重新发送</span>
          
        </div>
      </div>
      <div class="login-bottom">
        <div class="login-button">
          <span @click="register">注册</span>
        </div>
        <div class="register-button">
          <router-link to="/login"> <span>已有账号，去登录</span></router-link>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { Dialog } from "vant";
// import { mapState } from "vuex";
import Vue from "vue";
import '../http/request'
import { storeAllFunc } from '../http/store';
import { goodssearchFunc, mycartFunc } from '../http/request';

// 全局注册
Vue.use(Dialog);
export default {
  component: {
    [Dialog.Component.name]: Dialog.Component,
  },
  name: "register",
  data() {
    return {
      isfirstsend:true,
      isrestime:false,
      isresend:false,
      restime:60,
      reuser: {
        name: "",
        id: "",
        password: "",
        email: "",
        mycartnum: 0,
        checkcode:'',
        d:null
      },
    };
  },
  computed: {
    //    ...mapState(['user'])
  },
  methods: {
          // <span @click="sendemail">发送验证码</span>
          derestime(){
              if(this.restime>0){
                this.restime--
              }
              else{
          this.isrestime=false;
          this.isresend=true;
          this.d=null
              }
          }
          ,
    sendemail(){

      this.restime=60
      if(this.isfirstsend==true){
        this.isfirstsend=false;
      }
        this.isrestime=true;
        this.isresend=false;
        // console.log(this.isfirstsend,isrestime,is);
         mycartFunc('/sendEmail',{email:this.reuser.email}).then(response=>{
           console.log(response);
         })
         if(this.restime>0){
          this.d=setInterval(this.derestime,10);
         }
    } ,
    register() {
      axios({
        methods: "get",
        url: "http://127.0.0.1:3005/register",
        params: {
          ...this.reuser,
        },
      }).then((response) => {
        console.log(response);
        console.log(this.reuser);
        // console.log(response.data.result);
        // let result = response.data.status;
        // let { userid, username, email } = result;
        // console.log(result);
        let {code,userid}=response.data
        if (code==200){
          localStorage.setItem('userid',userid)
          Dialog.alert({
            title: "注册成功即将跳转个人中心页面?",
            //   message: 'data.msg',
          }).then(() => {
            // 页面跳转(传递参数)
            this.$router.push({
              name: "login",
              // params: { userid, username, email },
            });
          });
        } else {
          Dialog.alert({
            title: "注册失败",
            //   message: 'data.msg',
          }).then(() => {
          
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  box-sizing: border-box;
  width: 355px;
  margin: 50px auto;

  border: 2px solid #fafafa;
  .login-head {
    box-sizing: border-box;
    color: #777;
    width: 355px;
    height: 40px;
    margin: 50px auto;
  }
  .email {
    margin: 15px 0px;
    input {
     width: 200px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
      color: #777;
    }
  }
  .checkcode{
     display: flex;
     justify-content: center;
     margin: 15px 0px;
    input {
     width: 100px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
      color: #777;
    }
    span{
      display: block;
      width: 90px;
      height: 30px;
      line-height: 30px;
      border-radius: 8px;
      font-size: 16px;
      color: #777;
      border: 1px solid #777;
      margin-left: 10px;
    }
  }
  .password {
   input {
     width: 200px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
      color: #777;
    }
  }
  .usercount {
    border: none;
    margin-bottom: 20px;
    input {
      width: 200px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
      color: #777;
    }
  }
  .login-button {
    width: 250px;
    border-radius: 30px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    // margin-top:10px;
    margin: 30px auto;
    color: #fff;
    font-weight: 808;
    background-color: orange;
  }
  .register-button {
    width: 250px;
    border-radius: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    margin: 0 auto;
    background-color: #777;
    color: #ccc;
    margin-bottom: 10px;
  }
}
</style>
